// 
var gulp = require('gulp');
// rollup构建js
var rollup = require('rollup');
var string = require('rollup-plugin-string');
var uglify = require('gulp-uglify');
var resolve = require('rollup-plugin-node-resolve');
var commonjs = require('rollup-plugin-commonjs');
// less
var less = require('gulp-less');
var base64 = require('gulp-base64');
var csso = require('gulp-csso');
// 
var rename = require("gulp-rename");



// config
var src = './src/';
var dist = './dist/';


// 默认
gulp.task('default', ['main', 'watch'], function(){
    console.log('default');
});

// 主任务
gulp.task('main', ['js', 'css'], function(){
  console.log('main');
});

// js主任务
gulp.task('js', ['jsrollup', 'jsmini'], function(){
  console.log('js');
});

// css主任务
gulp.task('css', ['less', 'base64', 'cssmin'], function(){
  console.log('css');
});

// 监听任务
gulp.task('watch', function(){
  gulp.watch(src + '**/**/*.less', ['css']);
  gulp.watch(src + '**/*.js', ['js']);
  gulp.watch(src + '**/*.html', ['js']);
})



// js rollup
gulp.task('jsrollup', async function () {
    const bundle = await rollup.rollup({
      input: src + 'iYox',
      context: 'window',
      plugins: [
        // resolve({
        //   jsnext: true
        // }),
        // commonjs(),
        string({
          // Required to be specified
          include: src + '/components/**/*.html',
        })
      ]
    });
    await bundle.write({
        name: 'iYox',
        format: 'umd',
        file: dist + 'iYox.js'
    });
});

// js mini
gulp.task('jsmini', ['jsrollup'], function(){
    return gulp.src(dist + 'iYox.js')
      .pipe(uglify())
      .pipe(rename({
        extname: ".min.js"
      }))
      .pipe(gulp.dest(dist));
});

// css less
gulp.task('less', function(){
    return gulp.src(src + 'iYox.less')
      .pipe(less())
      .pipe(gulp.dest(dist))
});

// css base64
gulp.task('base64', ['less'], function(){
  return gulp.src(dist + 'iYox.css')
    .pipe(base64())
    .pipe(gulp.dest(dist));
});

// css min
gulp.task('cssmin', ['base64'], function(){
  return gulp.src(dist + 'iYox.css')
    .pipe(csso())
    .pipe(rename({
      extname: ".min.css"
    }))
    .pipe(gulp.dest(dist));
});